package dyyx.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public abstract class CommUtil {

	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String UTF8 = "utf8";

	public static void main(String[] args) throws Exception {

	}

	public static boolean isNotBlank(final CharSequence cs) {
		return !isBlank(cs);
	}

	public static boolean isBlank(final CharSequence cs) {
		int strLen;
		if (cs == null || (strLen = cs.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(cs.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static String getDateString(Date date) {
		return format(date, DATE_FORMAT);
	}

	public static String getDateTimeString(Date date) {
		return format(date, DATE_TIME_FORMAT);
	}

	public static String format(Date date, String format) {
		if (date == null) {
			return null;
		}
		if (isBlank(format)) {
			format = DATE_TIME_FORMAT;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		} catch (Throwable e) {

			return null;
		}
	}

	public static int getInt(String str, int def) {
		if (isBlank(str)) {
			return def;
		}
		try {
			return Integer.parseInt(str);
		} catch (Throwable e) {
			return def;
		}
	}

	public static long getLong(String str, long def) {
		if (isBlank(str)) {
			return def;
		}
		try {
			return Long.parseLong(str);
		} catch (Throwable e) {
			return def;
		}
	}

	public static int getIntFromSystemProp(String name, int def) {
		if (name == null) {
			return def;
		}
		String str = System.getProperty(name);
		return getInt(str, def);
	}

	public static long getFromSystemProp(String name, long def) {
		if (name == null) {
			return def;
		}

		String str = System.getProperty(name);
		return getLong(str, def);
	}

	public static Properties readPropertiesFromClassPath(String name) {
		if (name == null) {
			return null;
		}

		Properties prop = new Properties();
		InputStream inStream = null;

		try {
			inStream = CommUtil.class.getClassLoader().getResourceAsStream(name);
			if(inStream==null){
				throw new Exception("resource not found,name="+name);
			}
			prop.load(inStream);
			return prop;
		} catch (Throwable e) {
			throw new RuntimeException("readProperties error,name=" + name, e);
		} finally {
			close(inStream);
		}
	}

	public static void close(Closeable obj) {
		if (obj == null) {
			return;
		}
		try {
			obj.close();
		} catch (Throwable e) {

		}
	}

	public static BufferedReader createBufferedReader(String file, String charset) throws Exception {
		if (isBlank(charset)) {
			charset = UTF8;
		}

		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));

		return br;
	}
	
	public static BufferedWriter createBufferedWriter(String file, String charset) throws Exception {
		if (isBlank(charset)) {
			charset = UTF8;
		}

		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));

		return bw;
	}
	
	///
	public static String toJsonString(Object obj) {
		if (obj == null) {
			return null;
		}
		return JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.PrettyFormat,
				SerializerFeature.IgnoreErrorGetter);

	}

}
